package com.cn.ccp.service.resourse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.RowMapper;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ResourseBean;
import com.cn.ccp.service.BasicService;
import com.cn.ccp.util.Contants;

/**
 * 数据字典管理service
 * @author Administrator
 *
 */
public class ResourseService extends BasicService{
	private Log log = LogFactory.getLog(ResourseService.class);

	/**
	 * 创建数据字典
	 * @param bo
	 * @return
	 */
	public boolean saveResourse(ResourseBean bo){
		log.info(bo);
		String sql = "insert into resource_type(resource_type_name,resource_type_des,resource_type ) values (?,?,?)";
		return jdbcTemplate.update(sql ,new Object[]{bo.getName(),bo.getDescription(),bo.getType()}) > 0;
	}
	

	/**
	 * 修改数据字典
	 * @param bo
	 * @return
	 */
	public boolean updateResourse(ResourseBean bo){
		log.info(bo);
		String sql = "update resource_type set resource_type_name = ?, resource_type_des = ?, resource_type = ? where id = ?";
		return jdbcTemplate.update(sql ,new Object[]{bo.getName(),bo.getDescription(),bo.getType(),bo.getId()}) > 0; 
	}
	
	public ResourseBean getResourseByID(String strId){
		String sql = "SELECT * FROM resource_type WHERE id = ?";
		return (ResourseBean) jdbcTemplate.queryForObject(sql, new Object[]{strId},  new ResourseMapper());
	}
	
	/**
	 * 删除数据字典
	 * @param id
	 * @return
	 */
	public boolean delResourse(String id){
		String sql = "delete from resource_type where id = ?";
		return jdbcTemplate.update(sql ,new Object[]{id}) > 0;
	}
	
	/**
	 * 查询数据字典
	 * @param type：分类id
	 * @param start：起始页码
	 * @param pagesize：页码数据
	 * @return
	 */
	public List pageQueryResourse(String type,int start, int pagesize) {
			String sql = "SELECT * FROM resource_type  where resource_type = ? limit ?,?";
			return jdbcTemplate.query(sql, new Object[] {type,start,pagesize},new ResourseMapper());
	}
	
	/**
	 * 查询数据字典
	 * @param type：分类id
	 * @param start：起始页码
	 * @param pagesize：页码数据
	 * @return
	 */
	public List pageQueryResourse(int start, int pagesize) {
			String sql = "SELECT * FROM resource_type  limit ?,?";
			return jdbcTemplate.query(sql, new Object[] {start,pagesize},new ResourseMapper());
	}
	
	public List pageQueryResourse(int type) {
		String sql = "SELECT * FROM resource_type  where resource_type = ? ";
		return jdbcTemplate.query(sql, new Object[] {type},new ResourseMapper());
}
	
	
	public int getCountResourse(String type) {
		String sql = "select count(1) from resource_type where resource_type = ? ";
		return jdbcTemplate.queryForInt(sql,new Object[]{type});
	}
	public int getCountResourse() {
		String sql = "select count(1) from resource_type ";
		return jdbcTemplate.queryForInt(sql);
	}
	
	/**
	 * 查看该
	 * @param id
	 * @param roleName
	 * @param facultyId：所在学院id
	 * @return
	 */
	public boolean resourseNameIsAlready(String type ,String name) {
		String sql = "SELECT * FROM resource_type  WHERE resource_type_name = ? and resource_type = ?";
		return jdbcTemplate.query(sql, new Object[] { type,name }, new ResourseMapper()).size() > 0;
	}
	
  	
	/**
	 * 资源封装类
	 * @author Administrator
	 *
	 */
	public class ResourseMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int i) throws SQLException {
			ResourseBean rolesBean = new ResourseBean();
			rolesBean.setId(rs.getInt("id"));
			rolesBean.setName(rs.getString("resource_type_name"));
			rolesBean.setDescription(rs.getString("resource_type_des"));
			rolesBean.setType(rs.getString("resource_type"));
			try {
				if(rolesBean.getType().equalsIgnoreCase(String.valueOf(Contants.DATA_DICTIONARY_CLASS_TYPE)))
					rolesBean.setTypeName(BaseAction.getInstance().getMessages("pages_resourse_type_course"));
				else if(rolesBean.getType().equalsIgnoreCase(String.valueOf(Contants.DATA_DICTIONARY_LANGUAGE_TYPE)))
					rolesBean.setTypeName(BaseAction.getInstance().getMessages("course_page_course_language_type"));
				else if(rolesBean.getType().equalsIgnoreCase(String.valueOf(Contants.DATA_DICTIONARY_TEACHER_TYPE)))
						rolesBean.setTypeName(BaseAction.getInstance().getMessages("user_page_label_teacher_type"));
				else if(rolesBean.getType().equalsIgnoreCase(String.valueOf(Contants.DATA_DICTIONARY_TEACHER_TITLE_TYPE)))
					rolesBean.setTypeName(BaseAction.getInstance().getMessages("user_page_label_positional"));
				else
					rolesBean.setTypeName(BaseAction.getInstance().getMessages("pages_resourse_type_other"));
			} catch (Exception e) {
			}
			return rolesBean;
		}
		
	}
}
